package com.example.zhuma.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.zhuma.R;

import java.util.ArrayList;
import java.util.List;

public class ImagePagerAdapter extends PagerAdapter {
    private final Context mContext;
    //聲明一個圖像試圖列表
    private List<ImageView> mViewList = new ArrayList<>();
    @Override
    public int getCount() {
        return mViewList.size();
    }

    public ImagePagerAdapter(Context mContext)
    {
        this.mContext = mContext;
        for (int i=0;i<2;i++)
        {
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            ));
            if(i==0)
            {
                imageView.setImageResource(R.drawable.image_pager1);

            }
            if(i==1)
            {
                imageView.setImageResource(R.drawable.image_pager2);
            }
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mViewList.add(imageView);
        }

    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView item = mViewList.get(position);
        container.addView(item);
        return item;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(mViewList.get(position));
    }
}
